﻿package com.lbynet.util {
	
	/**
	 * ...
	 * @lbynet  Currency.splitNumber(-1000000000.1234)  
	 * 								 -1,000,000,000.12339997291564941
	 */
	public class Currency {
		
		public function Currency() {
			
		}
		
		public static function splitNumber(number:Number):String {
			
			
			
			var num:Number = Math.abs(number);
			//var splitNum:int = (num > 0)?Math.floor(num):Math.ceil(num);
			var splitInt:int = Math.floor(num);
			var splitFloat:Number = num - splitInt;
			
			var stringInt:String = splitInt.toString();
			var stringFloat:String = splitFloat.toString();
			
			
			var int_length:uint = stringInt.length;
			var remainder_length:uint;
			var returnString:String = "";
			
			
			if ( int_length >= 4) {
				
				remainder_length = int_length % 3;
				if (remainder_length == 1) {
					
					stringInt = "00" + stringInt;
				}else if (remainder_length == 2) {
					
					stringInt = "0" + stringInt;
				}
				
				
				var new_length:uint = stringInt.length;
				for (var i:uint = 0; i <= new_length; i += 3) {
					
					returnString += stringInt.substr(i, 3);
					if (i < new_length-3) returnString += ",";
					
				}
				
				if (remainder_length != 0) returnString = returnString.substr( (3 - remainder_length), returnString.length);
			}else {
				
				returnString = stringInt;
			}
			
			//trace("==" + returnString);
			
			if (splitFloat != 0) {
				
				stringFloat = stringFloat.substr(1,stringFloat.length);
				returnString += stringFloat;
			}
			
			if (number < 0) returnString = "-" + returnString;
			
			//trace("==" + returnString);
			
			return returnString;
			
		}
		
		
	}
	
}